package mx.kolobok.noteeditor.ui.list;

import mx.kolobok.noteeditor.Note;
import mx.kolobok.noteeditor.NoteManagerException;
import org.apache.log4j.Logger;

import javax.swing.plaf.ListUI;
import java.awt.*;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.io.IOException;

/**
* User:  Nikita Belenkiy
* Date: 29.05.11
* Time: 13:17
*/
class MyDropTargetAdapter extends DropTargetAdapter {
    private int prevDraggingOver=-1;
    private NoteList noteList;
    private static final Logger logger = Logger.getLogger(MyDropTargetAdapter.class);

    public MyDropTargetAdapter(NoteList noteList) {
        this.noteList = noteList;
    }

    @Override
    public void dragOver(DropTargetDragEvent dtde) {
        Point location = dtde.getLocation();
        logger.debug(location);
        updateDraggingUI(location);
    }

    private void updateDraggingUI(Point location) {
        NoteList list = noteList;
        ListUI ui = noteList.getUI();
        noteList.cellRenderer.draggingOver = ui.locationToIndex(list, location);
        if (noteList.cellRenderer.draggingOver != prevDraggingOver) {
            Rectangle cellBounds = ui.getCellBounds(list, prevDraggingOver == -1 ? noteList.cellRenderer.draggingOver : prevDraggingOver, noteList.cellRenderer.draggingOver);
            list.repaint(cellBounds);
            prevDraggingOver = noteList.cellRenderer.draggingOver;
        }
    }

    @Override
    public void dropActionChanged(DropTargetDragEvent dtde) {
         Point location = dtde.getLocation();
        logger.debug(location);
        updateDraggingUI(location);
    }

    @Override
    public void dragExit(DropTargetEvent dte) {
         NoteList list = noteList;
        ListUI ui = noteList.getUI();
        noteList.cellRenderer.draggingOver=-1;
         Rectangle cellBounds = ui.getCellBounds(list,prevDraggingOver, prevDraggingOver);
            list.repaint(cellBounds);
    }

    @Override
    public void drop(DropTargetDropEvent dtde) {
        Transferable transferable = dtde.getTransferable();
        boolean dataFlavorSupported = transferable.isDataFlavorSupported(NoteList.NOTE_DATA_FLAVOR);
        if (dataFlavorSupported) {

            try {
                Note note = (Note) transferable.getTransferData(NoteList.NOTE_DATA_FLAVOR);
                 ListUI ui = noteList.getUI();
        int newIndex = ui.locationToIndex(noteList, dtde.getLocation());
                logger.info("transferData = " + note);

                noteList.cellRenderer.draggingOver=-1;
                noteList.getNoteListModel().moveNoteToPosition(note, newIndex);
            } catch (UnsupportedFlavorException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NoteManagerException e) {
                e.printStackTrace();
            }
        }

    }
}
